namespace hjc {

	export class SystemManager extends Singleton implements ILaunchable {

		private $systems: System[] = [];
		private static $dftClazzs = [hjc.AStarSystem, hjc.MoveSystem, hjc.ScanSystem,
		hjc.RenderSystem, hjc.AoiSystem, hjc.IndependaceSystem, hjc.SingletonSystem];
		public launch(options: LaunchOptions): SystemManager {
			let arr = Array.isArray(options.Systems) ? options.Systems.unique() : [];
			if (!!options.ecs) arr.push(...SystemManager.$dftClazzs);
			if (notEmptyArr(arr)) {
				arr.forEach((clazz: new () => System) => this.$systems.push(new clazz()));
				game.timer.registerByFrames(1, (param?: TimeParam) => this.$systems.forEach(system => {
					if (system.enabled) system.onFrame(param);
				}));
			}
			return this;
		}

		public addIndpComp<C extends hjc.Component>(indpComp: C): void {
			this.get(IndependaceSystem).addComp(indpComp);
		}

		public get<S extends System>(clazz: new () => S): S {
			for (let system of this.$systems) {
				if (system instanceof clazz) return system;
			}
			let system = new clazz();
			this.$systems.push(system);
			return system;
		}

		public getAll(): System[] { return this.$systems; }
	}

}